<?php
/**
 * User: hjd-yao
 * Date: 2019/4/2
 * Time: 16:40
 */

namespace DC\UserCenter\Services;


use App\Models\Bonus\BonusRecordModel;
use App\Models\Rule\BonusRuleModel;
use App\Models\User\AccountFlowModel;
use Carbon\Carbon;
use DC\BonusCenter\Services\RegBonus;
use DC\Support\Traits\DCResponse;
use DC\UserCenter\Models\UserAuthModel;
use DC\UserCenter\Models\UserBaseModel;
use DC\UserCenter\Models\UserWalletModel;

class UserActivate
{
    use DCResponse;

    public function activating($id,$activated_id = 0)
    {
        $user = UserBaseModel::find($id);
        if(!$user||$user->status!=0){
            return $this->error('会员不存在或已经被激活');
        }
        $auth = UserAuthModel::find($id);
        $user->status = $user->is_active = $auth->status = 1;
        $user->active_type = 1 ;
        $user->activated_at = $auth->activated_at= Carbon::now()->getTimestamp();
        $auth->activated_id = $activated_id;
        $auth->activated_ip = ip2long(request()->getClientIp());
        if($user->save()&&$auth->save()){
            $this->award($id);
            return $this->success(['id'=>$id]);
        }
        return $this->error('失败');
    }


    public function EmptyActivating($id,$activated_id = 0)
    {
        $user = UserBaseModel::find($id);
        if(!$user||$user->status!=0){
            return $this->error('会员不存在或已经被激活');
        }
        $auth = UserAuthModel::find($id);
        $user->status = $user->is_active = $auth->status = 1;
        $user->active_type = 2 ;
        $user->activated_at = $auth->activated_at= Carbon::now()->getTimestamp();
        $auth->activated_id = $activated_id;
        $auth->activated_ip = ip2long(request()->getClientIp());
        if($user->save()&&$auth->save()){
            $this->award($id);
            return $this->success(['id'=>$id]);
        }
        return $this->error('失败');
    }

    // 前台会员激活

    public function activatingByUser($id,$activated_id = 0)
    {
        $user_info = UserBaseModel::init()->find($activated_id);

        $no_user = UserBaseModel::init()->find($id);

        if(!$no_user||$no_user->status!=0){
            return $this->error('会员不存在或已经被激活');
        }

        $bc_reg=BonusRuleModel::where('name','reg')->first();
        $bill_money=$bc_reg->rule1;

//        $bill_money_rule = format_rule(json_decode(get_rule('bill_money')));
//        $bill_money = $bill_money_rule[$no_user->level][1] ?? 0 ;

        $money_type = config('website')['bill_money_type'] ?? 1;
        $field = "money".$money_type;
        if($user_info->wallet->$field < $bill_money){
            return $this->error('激活账户余额不足，无法激活该会员！！！');
        }

        $auth = UserAuthModel::init()->find($id);
        $no_user->status = $no_user->is_active = $auth->status = 1;
        $no_user->active_type = 1 ;
        $no_user->activated_at = $auth->activated_at= Carbon::now()->getTimestamp();
        $auth->activated_id = $activated_id;
        $auth->activated_ip = ip2long(request()->getClientIp());

        if($no_user->save()&&$auth->save()){

            if(!UserWalletModel::where('member_id',$activated_id)->decrement($field,$bill_money)){
                $no_user->status = $no_user->is_active = $auth->status = 0;
                $no_user->save()&&$auth->save();
                return $this->error('扣除报单账户余额失败，请稍后再试！！！');
            }

            $params = [
                'money_type'=>$money_type,
                'change_type'=>38,
                'before_money'=>$user_info->wallet->$field,
            ];
            AccountFlowModel::init()->createFlow($activated_id,$bill_money,0,$params);

            $this->award($id);

            return $this->success(['id'=>$id]);
        }
        return $this->error('失败');
    }


    public function freezing($id,$freeze_cause)
    {
        $user = UserBaseModel::find($id);
        $auth = UserAuthModel::find($id);
        if(!$user||$user->status==-1){
            return $this->error('会员不存在或已被冻结');
        }
        $user->status = $auth->status = -1;
        $auth->frozen_at = Carbon::now()->getTimestamp();
        $auth->frozen_cause = $freeze_cause;
        $auth->frozen_ip = ip2long(request()->getClientIp());
        if($auth->save()&&$user->save()){
            return $this->success(['id'=>$id]);
        }
        return $this->error('冻结失败');
    }

    public function unfreezing($id)
    {
        $user = UserBaseModel::find($id);
        $auth = UserAuthModel::find($id);
        if(!$user||$user->status!=-1){
            return $this->error('会员不存在或未被冻结');
        }
        $user->status = $auth->status = 1;
        if($auth->save()&&$user->save()){
            return $this->success(['id'=>$id]);
        }
        return $this->error('解冻失败失败');
    }


    // 激活 奖金程序
    private function award($new_uid)
    {
        $group=BonusRecordModel::max('group');
        if(!$group) {
            $group=1;
        }else{
            $group =$group+1;
        }
        RegBonus::make()->setGroup($group)->award($new_uid);
    }
}